#include <stdio.h>
#include <wiringPi.h>

#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5

PI_THREAD(Huxi_Thread)   		//申明子线程
{
    printf("PI_THREAD(Huxi_Thread)\n");
    int t = 20000;
    int ton = 0;  
    while(1)				//进入主逻辑循环
    {
	piLock(0);
	for(ton = 0; ton < t; ton+=100)	
	{
		digitalWrite(LED4, HIGH);      	//GPIO.5输出高电平
		delayMicroseconds(ton);
		digitalWrite(LED4, LOW);      	//GPIO.5输出高电平
		delayMicroseconds(t-ton);
	}
	for(ton = t; ton > 0; ton-=100)	
	{
		digitalWrite(LED4, HIGH);      	//GPIO.5输出高电平
		delayMicroseconds(ton);
		digitalWrite(LED4, LOW);      	//GPIO.5输出高电平
		delayMicroseconds(t-ton);
	}
	piUnlock(0);	
	delay(100);
    }
}

PI_THREAD(LED4_Thread)   		//申明子线程
{
    printf("PI_THREAD(LED4_Thread)\n");
    while(1)				//线程逻辑循环
    { 
	piLock(0);
        digitalWrite(LED4, LOW);
 	delay(500);  
	digitalWrite(LED4, HIGH);
 	delay(500); 
	digitalWrite(LED4, LOW);
 	delay(500);  
	digitalWrite(LED4, HIGH);
 	delay(500); 
	piUnlock(0);
	delay(100);  
    }
}

int main(int argc, char *argv[])
{
    if(wiringPiSetup() == -1)    	//初始化必须写在最前边
	{
        printf("setup wiringPi failed !");
        return 1;
    }
    printf("Hello World!\n");
    pinMode(LED1, OUTPUT);		//设置GPIO.5为输出模式
    pinMode(LED2, OUTPUT);	
    pinMode(LED3, OUTPUT);	
    pinMode(LED4, OUTPUT);

    //创建子线程
    int res = piThreadCreate(Huxi_Thread);
    if(res != 0)
    {
        printf("Huxi didn't start\n");
    }
    res = piThreadCreate(LED4_Thread);
    if(res != 0)
    {
        printf("LED4 didn't start\n");
    }
	
    int v = 0;
    while(1)				//进入主逻辑循环
    {
	v=!v;
	digitalWrite(LED1, v);      	//GPIO.5输出高电平
	delay(500);   
    }
    return 0;
}


